package com.tsinghuabigdata.edu.ddmath.module.neteaseim.fragment;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.netease.nimlib.sdk.chatroom.model.ChatRoomInfo;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tsinghuabigdata.edu.ddmath.R;
import com.tsinghuabigdata.edu.ddmath.commons.newbieguide.ScreenUtils;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.base.ui.TFragment;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.doodle.ActionTypeEnum;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.doodle.DrawBoardView;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.doodle.SupportActionType;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.doodle.Transaction;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.doodle.action.MyPath;
import com.tsinghuabigdata.edu.ddmath.util.DensityUtils;
import com.tsinghuabigdata.edu.ddmath.util.ToastUtils;

import org.xutils.common.util.DensityUtil;

import static android.os.Looper.getMainLooper;

/**
 * Created by hzxuwen on 2016/10/27.
 */

public class TestRTSFragment extends TFragment implements View.OnClickListener, DrawBoardView.FlipListener {
    private static final String TAG = TestRTSFragment.class.getSimpleName();
    private View         rootView;
    private LinearLayout mLlDoodle;
    private DrawBoardView doodleView; // 画板

    private LinearLayout mLlPlayBack;
    private LinearLayout mLlPaint;
    private TextView     tvPenTurn;
    private Context      mContext;

    //private EditText mEditText;
    private Button       mButtonUp;
    private Button       mButtonDown;
    private ImageView    mIvTest;
    // data
    private String       sessionId; // 白板sessionId
    private ChatRoomInfo roomInfo;
    //    private HashMap<Integer, Integer> colorChoosedMap = new HashMap<>();
    //    private HashMap<Integer, Integer> colorMap = new HashMap<>();
    private int          choosedColor;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.test_rts_fragment, container, false);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mContext = getActivity();
        findViews();
        setListener();
    }


    private void findViews() {
        // 画板
        mLlDoodle = findView(R.id.ll_doodle);
        doodleView = findView(R.id.doodle_view);
        // 控制条
        mLlPlayBack = findView(R.id.ll_play_back);
        mLlPaint = findView(R.id.ll_paint);
        tvPenTurn = findView(R.id.tv_pen_turn);

        //mEditText = findView(R.id.editText);
        mButtonUp =  findView(R.id.button_up);
        mButtonDown = findView( R.id.button_down );
        mIvTest = findView( R.id.iv_test );
        mLlPaint.setActivated(true);


        tvPenTurn.setActivated(true);
        doodleView.setEnableView(true);

        ViewGroup.LayoutParams layoutParams = mLlDoodle.getLayoutParams();

        int videoHeight = ScreenUtils.getScreenContentHeight(mContext) - DensityUtils.dp2px(mContext, 20 + 84);

        int maxHeight = ScreenUtils.getScreenContentHeight(mContext) - DensityUtils.dp2px(mContext, 20 + 48 + 20);
        int maxWidth = ScreenUtils.getScreenWidth(mContext) - videoHeight / 2 - DensityUtils.dp2px(mContext, 20 * 3);
        Log.i("Doodle", "maxHeight=" + maxHeight + " maxWidth=" + maxWidth);
        int realWidth;
        if (maxWidth * 5 / 7 > maxHeight) {
            layoutParams.width = maxHeight * 7 / 5;
            realWidth = maxHeight * 7 / 5;
            int disWidth = maxWidth - realWidth;
            initDoodleView("sfsd",disWidth/2,0);
            Log.i("Doodle", "disWidth=" + disWidth);
        } else {
            layoutParams.height = maxWidth * 5 / 7;
            realWidth = maxWidth;
            initDoodleView("sfsd",0,0);
        }
        Log.i("Doodle", "width=" + layoutParams.width + " height=" + layoutParams.height);
        mLlDoodle.setLayoutParams(layoutParams);


//        ViewGroup.LayoutParams params = doodleView.getLayoutParams();
//        params.height = realWidth * 297 / 210;
//        params.width  = realWidth;
//        Log.i("sky", "realWidth=" + realWidth + " params.height=" + params.height);
//        doodleView.setLayoutParams(params);
        String uri = "http://n.sinaimg.cn/sports/2_img/upload/cf0d0fdd/20170328/Vvcq-fyctevp8954777.jpg";
        ImageLoader.getInstance().displayImage(uri,mIvTest);
        mButtonUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean success = doodleView.scrollUp();
                if(!success)ToastUtils.showShort( mContext, "不能向上移动" );
            }
        });
        mButtonDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean success = doodleView.scrollDown();
                if(!success)ToastUtils.showShort( mContext, "不能向下移动" );
            }
        });
    }

    private void setListener() {
        mLlPlayBack.setOnClickListener(this);
        mLlPaint.setOnClickListener(this);
    }

    private void initDoodleView(String account, final int left, final int top) {
        Toast.makeText(getContext(), "init doodle success", Toast.LENGTH_SHORT).show();
        // add support ActionType
        SupportActionType.getInstance().addSupportActionType(ActionTypeEnum.Path.getValue(), MyPath.class);
        doodleView.init(sessionId, account, DrawBoardView.Mode.BOTH, Color.WHITE, Color.BLACK, getContext(), this);

        doodleView.setPaintSize(3);
        doodleView.setPaintType(ActionTypeEnum.Path.getValue());

        // adjust paint offset
        new Handler(getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                Rect frame = new Rect();
                getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
                int statusBarHeight = frame.top;
                Log.i("Doodle", "statusBarHeight =" + statusBarHeight);

                int marginTop = doodleView.getTop();
                Log.i("Doodle", "doodleView marginTop =" + marginTop);

                int marginLeft = doodleView.getLeft();
                Log.i("Doodle", "doodleView marginLeft =" + marginLeft);

                float offsetX = marginLeft+left+ DensityUtil.dip2px(20);
                float offsetY = statusBarHeight + marginTop + top+ DensityUtil.dip2px(44) + DensityUtil.dip2px(20) ;

                doodleView.setPaintOffset(offsetX, offsetY);
                Log.i("Doodle", "client1 offsetX = " + offsetX + ", offsetY = " + offsetY);
            }
        }, 50);
    }

    @Override
    public void onClick(View v) {
        if (v == mLlPaint) {
            //// TODO: 2017/3/21 控制画笔开关
            if (mLlPaint.isActivated()) {
                Log.i(TAG, "isActivated");
                mLlPaint.setActivated(false);
                doodleView.setEnableView(false);
                tvPenTurn.setActivated(false);
            } else {
                Log.i(TAG, "isNOTActivated");
                mLlPaint.setActivated(true);
                doodleView.setEnableView(true);
                tvPenTurn.setActivated(true);
            }
        } else if (v == mLlPlayBack) {
            doodleView.paintBack();
        }
    }

    @Override
    public void onFlipPage(Transaction transaction) {

    }
}
